package com.vegetable.modules.controller.outbound;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.PreventDuplicateSubmit;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.outbound.AllocationIn;
import com.vegetable.modules.entity.outbound.AllocationOrder;
import com.vegetable.modules.entity.outbound.AllocationOut;
import com.vegetable.modules.entity.outbound.po.AllocationInAddPO;
import com.vegetable.modules.entity.outbound.po.AllocationOrderAddPO;
import com.vegetable.modules.entity.outbound.po.AllocationOrderQueryPO;
import com.vegetable.modules.entity.outbound.po.AllocationOutAddPO;
import com.vegetable.modules.service.outbound.IAllocationOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 销售订货 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-27
 */
@Slf4j
@RestController
@RequestMapping("/allocation")
//@AllArgsConstructor
@Api(value = "产品调拨", tags = "产品调拨")
public class AllocationController extends AbstractController {
    @Autowired
    private IAllocationOrderService iAllocationOrderService;

    /**
     * 新增调拨订单
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增调拨订单", notes = "新增调拨订单")
    public R add(@RequestBody AllocationOrderAddPO po){
        log.info("条件查询销售订货列表{}", JSON.toJSONString(po));
        Integer res = iAllocationOrderService.addOrder(po);
        return R.data(res);
    }

    @PostMapping("/queryOrderList")
    @ApiOperation(value = "查询调拨订单", notes = "查询调拨订单")
    public R queryOrderList(@RequestBody AllocationOrderQueryPO po){
        log.info("条件查询销售订货列表{}", JSON.toJSONString(po));
        IPage<AllocationOrder> res = iAllocationOrderService.allocationList(po);
        return R.data(res);
    }

    @PostMapping("/queryInOrderList")
    @ApiOperation(value = "查询调拨入库订单", notes = "查询调拨入库订单", response = AllocationIn.class)
    public R queryInOrderList(@RequestBody AllocationOrderQueryPO po){
        log.info("查询调拨入库订单{}", JSON.toJSONString(po));
        IPage<AllocationIn> res = iAllocationOrderService.allocationInList(po);
        return R.data(res);
    }

    @PostMapping("/queryOutOrderList")
    @ApiOperation(value = "查询调拨出库订单", notes = "查询调拨出库订单", response = AllocationOut.class)
    public R queryOutOrderList(@RequestBody AllocationOrderQueryPO po){
        log.info("查询调拨出库订单{}", JSON.toJSONString(po));
        IPage<AllocationOut> res = iAllocationOrderService.allocationOutList(po);
        return R.data(res);
    }

    @GetMapping("/queryOrder")
    @ApiOperation(value = "查询调拨订单", notes = "查询调拨订单", response = AllocationOrder.class)
    public R queryOrder(@RequestParam(value = "orderId")Long orderId){
        AllocationOrder res = iAllocationOrderService.queryOrder(orderId);
        return R.data(res);
    }
    @GetMapping("/queryInOrder")
    @ApiOperation(value = "查询调拨入库订单详情", notes = "查询调拨入库订单详情", response = AllocationIn.class)
    public R queryInOrder(@RequestParam(value = "orderId")Long orderId){
        AllocationIn res = iAllocationOrderService.queryInOrder(orderId);
        return R.data(res);
    }

    @GetMapping("/queryOutOrder")
    @ApiOperation(value = "查询调拨出库订单详情", notes = "查询调拨出库订单详情", response = AllocationOut.class)
    public R queryOutOrder(@RequestParam(value = "orderId")Long orderId){
        AllocationOut res = iAllocationOrderService.queryOutOrder(orderId);
        return R.data(res);
    }

    @PostMapping("/saveAllocationIn")
    @ApiOperation(value = "保存调拨入库", notes = "保存调拨入库")
    public R saveAllocationIn(@RequestBody AllocationInAddPO po) throws Exception {
        log.info("查询调拨出库订单{}", JSON.toJSONString(po));
        iAllocationOrderService.saveAllocationIn(po);
        return R.data("");
    }

    @PostMapping("/submitAllocationIn")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "提交调拨入库", notes = "提交调拨入库")
    public R submitAllocationIn(@RequestBody AllocationInAddPO po) throws Exception {
        log.info("查询调拨出库订单{}", JSON.toJSONString(po));
        iAllocationOrderService.submitAllocationIn(po);
        return R.data("res");
    }
    @PostMapping("/saveAllocationOut")
    @ApiOperation(value = "保存调拨出库", notes = "保存调拨出库")
    public R saveAllocationOut(@RequestBody AllocationOutAddPO po) throws Exception {
        log.info("查询调拨出库订单{}", JSON.toJSONString(po));
        int res = iAllocationOrderService.saveAllocationOut(po);
        return R.data(res);
    }
    @PostMapping("/submitAllocationOut")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "提交调拨出库", notes = "提交调拨出库")
    public R submitAllocationOut(@RequestBody AllocationOutAddPO po) throws Exception {
        log.info("查询调拨出库订单{}", JSON.toJSONString(po));
        iAllocationOrderService.submitAllocationOut(po);
        return R.data("res");
    }
    @GetMapping("/delete")
    @ApiOperation(value = "删除调拨订单", notes = "删除调拨订单")
    public R delete(Long id) throws Exception {
        iAllocationOrderService.deleteOrder(id);
        return R.status(true);
    }
}
